﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Markup;
using UIT.Style;

namespace UIT.Controls.Containers
{
    [ContentProperty("Rows")]
    public class Grid : DrawableControl, IContainer
    {
        public T FindControl<T>(string id) where T : IControl
        {
            return (T)UIT.Context.Current.Application.ControlFinder.Find(id, this);
        }

        public Grid()
        {
            Rows = new ControlCollection<Row>(this);
            children = new ControlCollectionWrapper<IContainer, Row>(Rows);
        }

        public ControlCollection<Row> Rows
        {
            get;
            private set;
        }

        #region IContainer Members

        IDisposableObservableCollection<IContainer, IControl> children;

        IDisposableObservableCollection<IContainer, IControl> IContainer<IContainer, IControl>.Children
        {
            get { return children; }
        }

        #endregion

        public override bool IsContainer
        {
            get { return true; }
        }

        public override ControlType Type
        {
            get { return ControlType.Table; }
        }
    }

    [ContentProperty("Child")]
    public class Cell : Control, IContainer, IContainable<Row>
    {
        private SingleControlCollection<IControl> children;
        public Cell()
        {
            children = new SingleControlCollection<IControl>(this);
        }

        public IControl Child
        {
            get { return children.Child; }
            set { children.Add(value); }
        }

        #region IContainer Members



        IDisposableObservableCollection<IContainer, IControl> IContainer<IContainer, IControl>.Children
        {
            get { return children; }
        }

        #endregion

        public override bool IsContainer
        {
            get { return true; }
        }

        public override ControlType Type
        {
            get { return ControlType.TableCell; }
        }

        #region IContainable<Row> Members

        Row IContainable<Row>.Parent
        {
            get
            {
                return (Row)base.Parent;
            }
            set
            {
                base.Parent = value;
            }
        }

        #endregion
    }

    [ContentProperty("Cells")]
    public class Row : Control, IContainer
    {
        public Row()
        {
            Cells = new ControlCollection<Cell>(this);
            children = new ControlCollectionWrapper<IContainer, Cell>(Cells);
        }

        public Unit Height { get; set; }
        public Unit MinHeight { get; set; }
        public Unit MaxHeight { get; set; }

        #region IContainer Members

        IDisposableObservableCollection<IContainer, IControl> children;

        IDisposableObservableCollection<IContainer, IControl> IContainer<IContainer, IControl>.Children
        {
            get { return children; }
        }

        #endregion

        public override bool IsContainer
        {
            get { return true; }
        }

        public override ControlType Type
        {
            get { return ControlType.TableRow; }
        }

        public ControlCollection<Cell> Cells { get; set; }
    }

}
